package rs.fon.rapidminer.parameter;

import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;

import rs.fon.rapidminer.gui.MainPanel;
import rs.fon.rapidminer.process.builder.ProcessBuilder;

import com.rapidminer.gui.properties.FileValueCellEditor;
import com.rapidminer.operator.Operator;

/**
 * View of the {@link ParameterTypeProcessFile}
 * 
 * @author Nikola Nikolic
 * 
 */
public class ProcessFileValueEditor extends FileValueCellEditor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JButton processButton;

	private ParameterTypeProcessFile type;

	JDialog dialog = null;

	public ProcessFileValueEditor(ParameterTypeProcessFile type) {
		super(type);
		this.type = type;

		processButton = new JButton("Configure process");
		addAction();
		addButton(createFileChooserButton(), GridBagConstraints.WEST);
		addButton(processButton, GridBagConstraints.REMAINDER);

		// TODO Auto-generated constructor stub
	}

	public void setOperator(Operator operator) {
		// TODO Auto-generated method stub

	}

	private void addAction() {
		processButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ProcessBuilder pb = null;
				try {
					pb = type.getProcessBuilder().newInstance();
					if (dialog == null) {
						dialog = new MainPanel(pb);
						dialog.setModal(true);
						dialog.setVisible(true);
					}
				} catch (InstantiationException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IllegalAccessException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});
	}

}
